package com.weitu.accidentEvent.event.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.accidentEvent.accident.mapper.CasualtiesMapper;
import com.weitu.accidentEvent.event.mapper.EventMapper;
import com.weitu.accidentEvent.event.service.EventService;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by fanyuting on 2017/6/1 0001.
 */
@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private CasualtiesMapper casualtiesMapper;

    @Override
    public List<Map<String, Object>> queryListByParam(Map<String, Object> map) {
        return this.eventMapper.queryListByParam(map);
    }

    @Override
    public int queryListCountByParam(Map<String, Object> map) {
        return this.eventMapper.queryListCountByParam(map);
    }

    @Override
    public Map<String, Object> queryInfoById(String id) {
        return this.eventMapper.queryInfoById(id);
    }

    @Transactional
    @Override
    public boolean addInfo(Map<String, Object> map) {
        /**
         * 保存行编辑数据
         */
        String listStr1 = "";
        if (map.get("casualtiesList") != null) {
            listStr1 = map.get("casualtiesList").toString();
        }
        List<Map> list1 = JSONArray.parseArray(listStr1, Map.class);
        for (Map m : list1) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        Map<String, Object> map1 = new HashedMap();
        map1.put("list", list1);
        map1.put("accidentReportId", map.get("id"));
        int result = this.eventMapper.insertInfo(map);
        int result1 = 0;
        if (result >= 1) {
            result1 = this.casualtiesMapper.insertInfo(map1);
        }
        if (result < 1) {
            return false;
        }
        return true;
    }

    @Transactional
    @Override
    public boolean editInfo(Map<String, Object> map) {
        int result = this.eventMapper.updateInfo(map);
        //删除数据
        String accidentReportId = map.get("id").toString();
        if (result >= 1) {
            this.casualtiesMapper.deleteInfo(accidentReportId);
        }
        /**
         * 保存行编辑数据
         */
        String listStr1 = "";
        if (map.get("casualtiesList") != null) {
            listStr1 = map.get("casualtiesList").toString();
        }
        List<Map> list1 = JSONArray.parseArray(listStr1, Map.class);
        for (Map m : list1) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        Map<String, Object> map1 = new HashedMap();
        map1.put("list", list1);
        map1.put("accidentReportId", map.get("id"));
        int result1 = 0;
        if (result >= 1) {
            result1 = this.casualtiesMapper.insertInfo(map1);
        }
        if (result < 1) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteInfo(String id) {
        int result = this.eventMapper.deleteInfo(id);
        if (result < 1) {
            return false;
        }
        return true;
    }

    @Override
    public boolean editAppState(String id, String appState) {
        int result = this.eventMapper.updateAppState(id, appState);
        if (result < 1) {
            return false;
        }
        return true;
    }
}
